import org.mozilla.javascript.*;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.tools.shell.Global;
import java.io.*;
import java.lang.*;

public class JSDoc extends ScriptableObject {
    // The zero-argument constructor used by Rhino runtime to create instances
    public JSDoc() { }

    // The class name is defined by the getClassName method
    public String getClassName() { return "JSDoc"; }

    /**
     * The Java method defining the JavaScript File constructor.
     *
     * If the constructor has one or more arguments, and the
     * first argument is not undefined, the argument is converted
     * to a string as used as the filename.<p>
     *
     * Otherwise System.in or System.out is assumed as appropriate
     * to the use.
     */
    public static Scriptable jsConstructor(Context cx, Object[] args,
                                           Function ctorObj,
                                           boolean inNewExpr)
    {
        JSDoc result = new JSDoc();
        AstRoot tree = null;
        String filename = "";
        FileReader file = null;
        String fileContents = "";
        
        try {
        
          if (args.length == 0 || args[0] == Context.getUndefinedValue()) {
              System.out.println("error - 1");
          } 
          else {
            filename = Context.toString(args[0]);
            file = new FileReader(filename);
          }
          fileContents = Kit.readReader(file);
          tree = parseJavascript(fileContents, filename);
          result.tree = tree;
        } 
        catch (FileNotFoundException ex) {
          Context.reportError("Couldn't open file \"" + filename + "\".");
        }
        catch (IOException ioe) {
          System.out.println("error - 2");
        }
        
        return result;
        
    }

     /**
     * Read the remaining lines in the file and return them in an array.
     *
     * Implements a JavaScript function.<p>
     *
     * This is a good example of creating a new array and setting
     * elements in that array.
     *
     * @exception IOException if an error occurred while accessing the file
     *            associated with this object
     */
    public Object jsFunction_functionCount()
        throws IOException
    {
        return "undefined function currently";
    }
    
    /**
     * Read the remaining lines in the file and return them in an array.
     *
     * Implements a JavaScript function.<p>
     *
     * This is a good example of creating a new array and setting
     * elements in that array.
     *
     * @exception IOException if an error occurred while accessing the file
     *            associated with this object
     */
    public Object jsFunction_toString()
        throws IOException
    {
        return tree.debugPrint();
    }
    
    
    /**
     * 
     * @param file
     * @return
     * @throws IOException
     * @author Glenn Boysko
     */
    private static AstRoot parseJavascript(String file, String fileName) throws
    IOException {
        // Setup the compiler environment, error reporter...
        CompilerEnvirons compilerEnv = new CompilerEnvirons();
        ErrorReporter errorReporter = compilerEnv.getErrorReporter();

        // Create an instance of the parser...
        Parser p = new Parser(compilerEnv, errorReporter);

        // Try to parse the reader...
        AstRoot ast = p.parse(file, fileName, 1);

        return ast;
    } 
    
    
    /* private members */
    private AstRoot tree = null;
    
}
